package com.foreknow.view;

import com.foreknow.model.Admin;
import com.foreknow.service.impl.AdminServiceImpl;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Register extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField passwordFiled;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Register frame = new Register();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Register() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u5BC6\u7801");
		lblNewLabel.setBounds(75, 155, 58, 15);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D");
		lblNewLabel_1.setBounds(75, 75, 58, 25);
		contentPane.add(lblNewLabel_1);
		
		textField = new JTextField();
		textField.setBounds(127, 77, 210, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		passwordFiled = new JTextField();
		passwordFiled.setBounds(127, 152, 210, 21);
		contentPane.add(passwordFiled);
		passwordFiled.setColumns(10);
		this.setVisible(true);
		JButton button = new JButton("\u786E\u5B9A");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String rename = textField.getText();
				String repassword = passwordFiled.getText();
				AdminServiceImpl adminService = new AdminServiceImpl();
				Admin admin = new Admin();
				admin.setPassword(repassword);
				admin.setAdminName(rename);
				boolean save = adminService.register(admin);
				if (save){
					System.out.println("注册成功");
					Register.this.dispose();
				}else {
					System.out.println("失败");
				}
			}
		});
		button.setBounds(177, 218, 97, 23);
		contentPane.add(button);
	}
}
